﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data.Linq;
using DTOAuction;
using System.Configuration;


namespace DAOAuction
{
    public class DAOLop
    {
        // thêm lớp mới vào:
        public int themLopMoi(int maHPMo)
        {
            int _ketqua = 1;
            try
            {
                LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
                db.LOP_add(maHPMo);
            }
            catch
            {
                _ketqua = 0;
            }
            return _ketqua;
        }

        public List<LOP_getallwithMaKhoaResult> layDanhSachLopTheoMaKhoa(int maKhoa)
        {
            List<LOP_getallwithMaKhoaResult> listLop = new List<LOP_getallwithMaKhoaResult>();
            try
            {
                LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
                ISingleResult<LOP_getallwithMaKhoaResult> lop = db.LOP_getallwithMaKhoa(maKhoa);

                foreach (LOP_getallwithMaKhoaResult l in lop)
                {
                    LOP_getallwithMaKhoaResult var1 = new LOP_getallwithMaKhoaResult();
                    var1.MALOP = l.MALOP;
                    var1.TENLOP = l.TENLOP;
                    var1.TENMONHOC = l.TENMONHOC;
                    var1.TIETBATDAU = l.TIETBATDAU;
                    var1.NGAYHOC = l.NGAYHOC;

                    listLop.Add(var1);
                }

            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listLop;   
        }

        public int update_PhanCongGiaoVien(int maLop, int maGiaoVien)
        {
            int ketqua = 0;
            try
            {
                LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
                db.LOP_update_PhanCongGiaoVien(maGiaoVien, maLop);
                ketqua = 1;
            }
            catch (Exception e)
            {
                ketqua = 0;
                throw e;                
            }
            return ketqua;
        }

        public int update_sapxepThoiKhoaBieu(int maLop, string ngay, int bd, int kt)
        {
            int ketqua = 0;
            try
            {
                LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
                db.LOP_updateThoiKhoaBieu(maLop, ngay, bd, kt);
                ketqua = 1;
            }
            catch (Exception e)
            {
                ketqua = 0;
                throw e;
            }
            return ketqua;
        }

        public List<LOP_LayDanhSachLopHocChuaCoPhongResult> layDanhSachLopChuaCoPhong()
        {
            List<LOP_LayDanhSachLopHocChuaCoPhongResult> lstLop = new List<LOP_LayDanhSachLopHocChuaCoPhongResult>();
            using (LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString))
            {
                ISingleResult<LOP_LayDanhSachLopHocChuaCoPhongResult> lp = db.LOP_LayDanhSachLopHocChuaCoPhong();
                foreach (LOP_LayDanhSachLopHocChuaCoPhongResult LOP in lp)
                {
                    LOP_LayDanhSachLopHocChuaCoPhongResult var1 = new LOP_LayDanhSachLopHocChuaCoPhongResult();
                    var1.MALOP = LOP.MALOP;
                    var1.TENLOP = LOP.TENLOP;
                    var1.MAPHONG = LOP.MAPHONG;
                    var1.MAGIAOVIEN = LOP.MAGIAOVIEN;
                    var1.MAHPMO = LOP.MAHPMO;
                    var1.NGAYHOC = LOP.NGAYHOC;
                    var1.TIETBATDAU = LOP.TIETBATDAU;
                    var1.TIETKETTHUC = LOP.TIETKETTHUC;
                    var1.SLSVDK_CHINHTHUC = LOP.SLSVDK_CHINHTHUC;
                    lstLop.Add(var1);
                    try
                    {
                        db.SubmitChanges();
                    }
                    catch
                    {
                        db.ChangeConflicts.ResolveAll(RefreshMode.KeepChanges);
                    }
                }
            }
            return lstLop;
        }

        // Cập nhật phòng vào lớp học
        public int capNhatPhongVaoLopHoc(int maLop, int maPhong)
        {
            int _ketQua = 1;
            try
            {
                LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
                db.LOP_CapNhatPhongVaoLopHoc(maLop, maPhong);
            }
            catch
            {
                _ketQua = 0;
            }
            return _ketQua;
        }

        public List<LOP_getallResult> layDanhSachTatCaCacLopDuocMo()
        {
            List<LOP_getallResult> listLop = new List<LOP_getallResult>();
            try
            {
                LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
                ISingleResult<LOP_getallResult> lop = db.LOP_getall();

                foreach (LOP_getallResult l in lop)
                {
                    LOP_getallResult var1 = new LOP_getallResult();
                    var1.MALOP = l.MALOP;
                    var1.TENLOP = l.TENLOP;
                    var1.MAHPMO = l.MAHPMO;
                    var1.NGAYHOC = l.NGAYHOC;
                    var1.TIETBATDAU = l.TIETBATDAU;
                    var1.TIETKETTHUC = l.TIETKETTHUC;
                    var1.NGAYHOC = l.NGAYHOC;
                    var1.MAGIAOVIEN = l.MAGIAOVIEN;
                    var1.MAPHONG = l.MAPHONG;
                    listLop.Add(var1);
                }
            }
            catch (Exception ex)
            {
                throw ex;
            }

            return listLop;
        }

		/// <summary>
		/// author: hqlam
		/// </summary>
		public static LOP layThongTinLop(int maLop)
		{
			try
			{
				LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
				return db.LOPs.First(lop => lop.MALOP == maLop);
			}
			catch
			{
				throw;
			}
		}

		/// <summary>
		/// author: hqlam
		/// </summary>
		public bool capNhatThongTinLopHoc(LOP lopHoc)
		{
			try
			{
				LinqDataContext db = new LinqDataContext(global::DAOAuction.Properties.Settings.Default.QUANLYGIAODUCConnectionString);
				LOP newLop = db.LOPs.First(l => l.MALOP == lopHoc.MALOP);
				newLop.SLSVDK_CHINHTHUC = lopHoc.SLSVDK_CHINHTHUC;
				db.SubmitChanges();
				return true;
			}
			catch
			{
				throw;
			}
		}
	}
}
